package com.mmall.seckill.service.impl;

import com.mmall.seckill.common.CodeMsg;
import com.mmall.seckill.common.Result;
import com.mmall.seckill.common.redis.RedisService;
import com.mmall.seckill.common.redis.keyCode.OrderKey;
import com.mmall.seckill.common.redis.keyCode.SeckillKey;
import com.mmall.seckill.dao.GoodsDao;
import com.mmall.seckill.dao.OrderDao;
import com.mmall.seckill.domain.OrderInfo;
import com.mmall.seckill.domain.SeckillGoods;
import com.mmall.seckill.domain.SeckillOrder;
import com.mmall.seckill.domain.SeckillUser;
import com.mmall.seckill.service.OrderService;
import com.mmall.seckill.util.JsonUtil;
import com.mmall.seckill.vo.GoodsVo;
import com.mmall.seckill.vo.OrderDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by megan on 2018/6/19.
 * 订单模块
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private RedisService redisService;
    @Resource
    private GoodsDao goodsDao;

    @Override
    public Result getOrderDetail(SeckillUser user,Long orderId){
        OrderInfo order = orderDao.getOrderById(orderId);
        if(order != null){
            long goodsId = order.getGoodsId();
            GoodsVo goods = goodsDao.getGoodsVoByGoodsId(goodsId);
            OrderDetailVO vo = new OrderDetailVO();
            vo.setOrderInfo(order);
            vo.setGoods(goods);
            return Result.success(vo);
        }
        return Result.coderMsg(CodeMsg.ORDER_NOT_EXIST);

    }

    @Override
    public OrderInfo getOrderById(Long orderId){
        return orderDao.getOrderById(orderId);
    }

    @Override
    public OrderInfo placeTheOrder(SeckillUser user, GoodsVo goods){
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setDeliveryAddrId(0L);
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsId(goods.getId());
        orderInfo.setGoodsName(goods.getGoodsName());
        orderInfo.setGoodsPrice(goods.getMiaoshaPrice());
        orderInfo.setOrderChannel(1);
        orderInfo.setStatus(0);
        orderInfo.setUserId(user.getId());
        long ret = orderDao.insert(orderInfo);
        if(ret > 0){
            SeckillOrder miaoshaOrder = new SeckillOrder();
            miaoshaOrder.setGoodsId(goods.getId());
            miaoshaOrder.setOrderId(orderInfo.getId());
            miaoshaOrder.setUserId(user.getId());
            orderDao.insertMiaoshaOrder(miaoshaOrder);
            // 写缓存
            redisService.set(OrderKey.getSeckillOrderByYidGid,user.getId()+"_"+goods.getId(), JsonUtil.obj2String(orderInfo));
            return orderInfo;
        }
        return null;
    }

    @Override
    @Transactional
    public OrderInfo miaoshaDb(SeckillUser user, GoodsVo goods){
        // 减少库存
        SeckillGoods g = new SeckillGoods();
        g.setGoodsId(goods.getId());
        int ret = goodsDao.reduceStock(g);
        OrderInfo orderInfo = new OrderInfo();
        if(ret > 0){
            orderInfo.setCreateDate(new Date());
            orderInfo.setDeliveryAddrId(0L);
            orderInfo.setGoodsCount(1);
            orderInfo.setGoodsId(goods.getId());
            orderInfo.setGoodsName(goods.getGoodsName());
            orderInfo.setGoodsPrice(goods.getMiaoshaPrice());
            orderInfo.setOrderChannel(1);
            orderInfo.setStatus(0);
            orderInfo.setUserId(user.getId());
            long res = orderDao.insert(orderInfo);
            if(res > 0){
                SeckillOrder miaoshaOrder = new SeckillOrder();
                miaoshaOrder.setGoodsId(goods.getId());
                miaoshaOrder.setOrderId(orderInfo.getId());
                miaoshaOrder.setUserId(user.getId());
                int rest = orderDao.insertMiaoshaOrder(miaoshaOrder);
                if(rest > 0){
                    // 写缓存
                    redisService.set(OrderKey.getSeckillOrderByYidGid,user.getId()+"_"+goods.getId(),JsonUtil.obj2String(orderInfo));
                    return orderInfo;
                }
            }
        }
        // 库存不足（卖完了）
        redisService.set(SeckillKey.isGoodsOver,goods.getId()+"_","true");
        return null;
    }
}
